/**
 * Created by admin on 2017/9/7.
 */
layui.use(['element', 'form', 'major', 'collage', 'pageFlip', 'global'], function () {
    var element = layui.element;
    var form = layui.form;
    $ = layui.jquery;
    var global = layui.global;
    var major = layui.major;
    var collage = layui.collage;
    var pageFlip = layui.pageFlip;

    var init = {
        findCollageList: function () {
            collage.findCollageList(null, function (resp) {
                if (resp.c == "100") {
                    var temp = '<option  value="">请选择学院</option>';
                    for (var i = 0; i < resp.d.l.length; i++) {
                        temp += '<option value="' + resp.d.l[i].collageId + '" >' + resp.d.l[i].collageName + '</option>';
                    }
                    $('#collage').html(temp);
                    form.render('select');
                } else {
                    layer.msg(resp.m);
                }
            })
        },
        findMajorList: function () {
            var obj = {
                currentPage: 1,
                collageId:$("#collage").val()
            }
            pageFlip.configure(obj, major.findMajorList, refreshTable, 'majorPageCtrl');
        }
    }
    init.findCollageList();
    init.findMajorList();
    var ue = UE.getEditor('introduction');

    form.on('select(collage)', function (data) {
        init.findMajorList();
    });

    function refreshTable(data, page) {
        var lines = [];
        for (var i = 0; i < data.length; i++) {
            var majorItem = data[i];
            var line = '<tr>';
            line += '<td>' + majorItem.majorId + '</td>';
            line += '<td>' + majorItem.majorName + '</td>';
            line += '<td>' + majorItem.years + '</td>';
            line += '<td>' + majorItem.degreeName + '</td>';
            line += '<td>' + majorItem.dormitory + '</td>';
            line += '<td>' + majorItem.introduction + '</td>';
            line += '<td>' + majorItem.createTime + '</td>';
            line += '<td>' + majorItem.updateTime + '</td>';
            line += '<td>' +
                '<button class="layui-btn layui-btn-normal edit" name="' + majorItem.majorId + '">编辑</button>' +
                // '<button class="layui-btn layui-btn-normal del" name="' + majorItem.majorId + '">删除</button>' +
                '<button class="layui-btn btn-disabled" name="' + majorItem.majorId + '">删除</button>' +
                '</td>';
            line += '</tr>';
            lines.push(line);
        }
        $('#majorTable tbody').empty().append(lines.join());
    }

    $("#addMajor ").click(function () {
        if(isNaN(parseInt($('#collage').val()))) {
            layer.msg("请选择学院后再添加专业");
            return;
        }
        $("#addMajorForm").trigger("reset");
        layer.open({
            type: 1,
            title: "新增专业",
            area: ['750px', '800px'],
            fix: false, //不固定
            maxmin: true,
            zIndex:1001,
            content: $("#majorForm"),
            btn: ["保存", "取消"],
            yes: function () {
                var param = global.form.serializeJSON($('#addMajorForm'));
                param.collageId = $('#collage').val();
                param.introduction = param.content == undefined ? "":param.content;
                major.saveMajor(param, function (resp) {
                    if (resp.c == "100") {
                        layer.closeAll();
                        init.findMajorList();
                    } else {
                        layer.msg(resp.m);
                    }
                })
            },
            btn2: function () {
                layer.msg("取消了");
                layer.closeAll();
            }
        });
    });
    $("#majorTable").delegate(".del", "click", function () {
        var majorId = $(this).attr("name");
        layer.confirm(
            "删除此专业吗？",
            ["确定", "取消"],
            function () {
                var param = {
                    "majorId": majorId,
                };
                major.deleteMajorById(param, function (resp) {
                    if (resp.c == "100") {
                        layer.msg("删除成功");
                        layer.closeAll();
                        init.findMajorList();
                    } else {
                        layer.msg(resp.m);
                    }
                })
            },
            function () {
                layer.msg("取消");
            }
        );
    });
    $("#majorTable").delegate(".edit", "click", function () {
        layer.open({
            type: 1,
            title: "编辑专业",
            area: ['750px', '800px'],
            fix: false, //不固定
            maxmin: true,
            content: $("#majorForm"),
            zIndex:1001,
            btn: ["保存", "取消"],
            yes: function () {
                var param = global.form.serializeJSON('#addMajorForm');
                param.introduction = param.content == undefined ? "":param.content;
                major.updateMajor(param, function (resp) {
                    if (resp.c == "100") {
                        layer.closeAll();
                        init.findMajorList();
                    } else {
                        layer.msg(resp.m);
                    }
                })
            },
            btn2: function () {
                layer.msg("取消了");
                layer.closeAll();
            }
        });
        var id = $(this).attr("name");
        var param = {
            "majorId": id
        };
        major.findMajorById(param, function (resp) {
            if (resp.c == "100") {
                var majorItem = resp.d;
                global.form.serializeByClass('addMajorForm',majorItem);
                form.render();
                ue.setContent(majorItem.introduction);
            } else {
                layer.msg(resp.m);
            }
        })

    })
});